<template>
  <div class="system-dic-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.params" inline>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button class="ml10" size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button class="ml10" size="default" type="success" @click="onOpenAddDic('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增属性
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" style="width: 100%"
                @sort-change="handleSortChange">
        <el-table-column label="序号" type="index" width="50"/>
        <el-table-column align="center" label="规格属性" prop="valueName"/>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip/>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEditDic('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="state.tableData.param.pageNum"
          v-model:page-size="state.tableData.param.pageSize"
          :page-sizes="[10, 20, 30]"
          :pager-count="5"
          :total="state.tableData.total"
          background
          class="mt15"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <specDialog ref="specDialogRef" :key-id="keyId" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemDicData" setup>
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {useRoute} from 'vue-router';
import {specApi} from "/@/api/store/commodity/spec";

// 引入组件
const specDialog = defineAsyncComponent(() => import('/@/views/store/commodity/spec/value/dialog.vue'));

// 定义变量内容
const specDialogRef = ref();
const queryFormRef = ref();
const keyId = ref();
const state = reactive<SysDicState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      params: {
        valueName: '',
        keyId: ''
      }
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  specApi()
      .getTableData(state.tableData.param)
      .then(res => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      })
};

const handleSortChange = (data: any) => {
  if (data.order) {
    if (data.order.indexOf('asc') > -1) {
      state.tableData.param.sortOrder = 'asc'
    } else {
      state.tableData.param.sortOrder = 'desc'
    }
    state.tableData.param.sortName = data.prop
  }
  getTableData();
};

// 打开新增字典弹窗
const onOpenAddDic = (type: string) => {
  specDialogRef.value.openDialog(type);
};

// 打开修改字典弹窗
const onOpenEditDic = (type: string, row: RowDicType) => {
  specDialogRef.value.openDialog(type, row);
};

// 删除字典
const onRowDel = (row: RowDicType) => {
  ElMessageBox.confirm(`此操作将永久删除字典名称：“${row.valueName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    specApi()
        .delRecord(row)
        .then(() => {
          getTableData();
          ElMessage.success('删除成功');
        })
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  // 獲取父ID
  const route = useRoute();
  keyId.value = route.params && route.params.id;
  state.tableData.param.params.keyId = keyId.value;
  getTableData();
});

// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};


</script>
